#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>


typedef struct ListNode {
    int val;
    struct ListNode* next;
}listnode;

struct ListNode* Buysltnode(int n)
{
    struct ListNode* tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (tmp == NULL)
    {
        perror("malloc Buysltnode");
        exit(-1);
    }
    tmp->val = n;
    tmp->next = NULL;
    return tmp;
}

struct ListNode* Creatlist(int n, int arr[20])
{
    struct ListNode* front = NULL;
    struct ListNode* tmp = NULL;
    int i = 0;
    for (i = 0; i < n; i++)
    {
        struct ListNode* new = Buysltnode(arr[i]);
        if (new == NULL)
        {
            perror("malloc Buysltnode");
            exit(-1);
        }
        if (front == NULL)
        {
            front = new;
            tmp = new;
        }
        else
        {
            tmp->next = new;
            tmp = tmp->next;
        }
    }
    return front;
}

void SLTprint(struct ListNode* head)
{
    struct ListNode* tmp = head;
    while (tmp)
    {
        printf("%d ", tmp->val);
        tmp = tmp->next;
    }
    printf("\n");
}


struct ListNode* ReverseList(struct ListNode* pHead)
{
    struct ListNode* p = pHead;
    struct ListNode* q = NULL;
    struct ListNode* temp = NULL;
    while (p)
    {
        temp = q;
        q = p;
        p = p->next;
        q->next = temp;
    }
    // write code here
    return q;
}

int main()
{
    int arr1[20] = { 1,2,4 };
    struct ListNode* new1 = Creatlist(3, arr1);
    SLTprint(new1);
    struct ListNode* new2 = ReverseList(new1);
    SLTprint(new2);


    return 0;
}
